package entity;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xb.yang
 * @Date: 2024/06/13/14:45
 * @Description:
 */

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

public class JGitExample {
    public static void main(String[] args) {
        try {
            // 1. 打开.git文件
            Repository repository = new FileRepositoryBuilder()
                    .setGitDir(new File("D:\\myProject\\untitled\\.git"))
                    .build();

            // 2. 获取当前分支名称
            String currentBranch = repository.getBranch();


            // 3. 执行git diff命令
            List<DiffEntry> diffs = getDiff(repository, currentBranch, "master");

            // 4. 将不同的地方写入文件
            writeDiffsToFile(diffs);

            repository.close();
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
    }

    private static List<DiffEntry> getDiff(Repository repository, String branch1, String branch2) throws GitAPIException, IOException {
        try (Git git = new Git(repository)) {
            ObjectId objectId1 = repository.resolve(branch1);
            ObjectId objectId2 = repository.resolve(branch2);
            if (objectId1 == null || objectId2 == null) {
                System.err.println("One or both branch names are invalid.");
                return null; // or handle the error appropriately
            }
            return git.diff()
                    .setOldTree(prepareTreeParser(repository, objectId1))
                    .setNewTree(prepareTreeParser(repository, objectId2))
                    .call();
        }
    }

    private static void writeDiffsToFile(List<DiffEntry> diffs) throws IOException {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             PrintStream printStream = new PrintStream(outputStream);
             DiffFormatter formatter = new DiffFormatter(outputStream)) {

            formatter.setDiffComparator(RawTextComparator.DEFAULT);
            formatter.setRepository(null);

            for (DiffEntry entry : diffs) {
                formatter.format(entry);
                printStream.flush();
                System.out.println(outputStream.toString());
                // 将outputStream中的内容写入文件中，这里只是示例，你可以根据需要修改
                // 将 outputStream.toString() 的内容写入你的目标文件
                // ...
                outputStream.reset();
            }
        }
    }

    private static AbstractTreeIterator prepareTreeParser(Repository repository, ObjectId objectId) throws IOException {
        try (RevWalk walk = new RevWalk(repository)) {
            RevTree tree = walk.parseTree(objectId);
            CanonicalTreeParser treeParser = new CanonicalTreeParser();
            try (ObjectReader reader = repository.newObjectReader()) {
                treeParser.reset(reader, tree);
            }
            walk.dispose();
            return treeParser;
        }
    }


}
